<?php

namespace App\Services\SwitchPay;

use App\Models\Orders;
use App\Services\WechatApi\UnifiedOrder\JsapiFunc;
use App\Services\WechatApi\UnifiedOrder\UnifiedOrder;
use App\Services\WechatApi\WechatApiOauth;
use Illuminate\Http\Request;
use YQ\Weixin\YqWeixin;

/**
 * 微信公众号支付
 * 接入文档: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
 */
class WanSdkWxpayJssdk extends SwitchPayBase
{
    public function payInit($order, Request $request)
    {
        $appid = $this->gameChannel['jspay_appid'];
        $wx_openid = $order->channel_openid;

        // 统一下单
        $params = [
            'body'         => "{$order->game_id}-{$order->goods_name}", // 商品描述
            'out_trade_no' => $order->trade_no, // 商户订单号
            'total_fee'    => ceil($order->money * 100), // 金额 分
            'notify_url'   => $this->callbackUrl(), // 异步通知回调
            'attach'       => $order->trade_no, // 附加数据
            'openid'       => $wx_openid, // 公众号下用户的openid
        ];
        $mch_id = $this->gameChannel['jspay_mch_id'];
        $mch_key = $this->gameChannel['jspay_mch_key'];
        $res = UnifiedOrder::getInstance()->createOrder($appid, $mch_key, $mch_id, 'JSAPI', $params);
        if ($res === false) {
            return yqerr(6027);
        }

        $bridge_config = JsapiFunc::getInstance()->bridgeConfig($appid, $mch_key, $res['prepay_id']);
        return yqok([
            'bridge' => $bridge_config,
        ]);
    }

    // 支付回调
    public function payCb(Request $request, $cb_type)
    {
        $notify = $this->payNotify;
        $notify->cbType = $cb_type;

        $mch_key = $this->gameChannel['jspay_mch_key'];
        $xml = file_get_contents('php://input');
        $res = UnifiedOrder::getInstance()->payNotify($mch_key, $xml);
        if (!$res) {
            $notify->errno = 6023;
            return $notify;
        }

        // 微信用户唯一标识
        $openid = $res['openid'];
        // 我方订单
        $trade_no = $res['out_trade_no'];
        // 微信订单
        $channel_trade_no = $res['transaction_id'];
        // 订单金额 分
        $total_fee = $res['total_fee'];

        // 校验订单
        $order = Orders::where('trade_no', $trade_no)->first();
        if (!$order) {
            $notify->errno = 6020;
            return false;
        }

        // 校验充值金额
        if (abs($order->money - $total_fee / 100) > 1) {
            $notify->errno = 6024;
            return false;
        }

        $notify->order          = $order;
        $notify->tradeNo        = $trade_no;
        $notify->channelTradeNo = $channel_trade_no;
        $notify->mchId          = $this->gameChannel['jspay_mch_id'];
        $notify->openid         = $openid;

        return $notify;
    }

    // 失败返回
    public function payErr()
    {
        $notify = $this->payNotify;
        $mch_key = $this->gameChannel['jspay_mch_key'];
        $xml = UnifiedOrder::getInstance()->payNotifyReturn($mch_key, 'FAIL', lang($notify->errno));
        return $xml;
    }

    // 成功返回
    public function payOk()
    {
        $mch_key = $this->gameChannel['jspay_mch_key'];
        $xml = UnifiedOrder::getInstance()->payNotifyReturn($mch_key, 'SUCCESS', 'OK');
        return $xml;
    }
}
